<?php 

class BlogPostValidator extends AgaviValidator
{
    /**
     * Check that post with given "coordinates" exists.
     *
     * @return boolean
     */
    protected function validate()
    {
        $return = false;
        
        $arguments = $this->getArguments();
        if (!count($arguments)) {
            $this->throwError();
            return false;
        }
        
        /* @var $model Public_PostsModel */
        $model = $this->getContext()->getModel('Posts', 'Public');
        
        if (count($arguments)==1 && $arguments[0]=='post_id') {
            // post_id specified
            $post = $model->getPost($this->getData('post_id'));
            if ($post instanceof Post) {
                $return = true;
            }
        } else {
            // i.e. full post specs provided (date, shortcut)
            $params = array();
            foreach ($arguments as $key => $value) {
                $params[$value] = $this->getData($value);
            }
            $date = date_create(sprintf('%s-%s-%s', $params['year'], $params['month'], $params['day']));
            $post = $model->getPost(null, $params['shortcut'], $date);
            if ($post instanceof Post) {
                $return = true;
            }
        }
        
        return $return;
    }
}

?>